package nxu.it.controller;

import nxu.it.model.ApiResult;
import nxu.it.service.GreetingService;
import org.noear.solon.annotation.*;
import org.noear.solon.boot.web.MimeType;
import org.noear.solon.core.handle.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@Controller
public class MainController {
    @Inject
    GreetingService greetingService;

    @Mapping(value = "/", produces = MimeType.TEXT_HTML_VALUE)
    @Get
    public String index() {
        return "<h1>Yes, it works!</h1>";
    }

    @Mapping(value = "/hello", produces = MimeType.TEXT_HTML_VALUE)
    @Get
    public String hello(String name) {
        return greetingService.sayHello(name);
    }

    @Mapping(value = "/json", produces = MimeType.APPLICATION_JSON_UTF8_VALUE)
    @Get
    public ApiResult json() {
        return ApiResult.ok("请求成功");
    }

    @Mapping(value = "/render", produces = MimeType.TEXT_HTML_VALUE)
    @Get
    public ModelAndView render() {
        ModelAndView mv = new ModelAndView("test.ftl");
        mv.put("name", "张三");
        return mv;
    }
    @Mapping(value="/login",produces = MimeType.TEXT_HTML_VALUE)
    @Get
    public ModelAndView login(){
        Map<String, Object> data = new HashMap<>();
        ModelAndView mv = new ModelAndView("login.ftl");
        mv.put("data",data);
        return mv;
    }

    @Mapping(value = "/loginCheck",produces = MimeType.TEXT_HTML_VALUE)
    @Post
    public String loginCheck(String username,String password){
        if(username == null || username.equals(""))
            return "<h1>用户名不能为空</h1>";
        if(password == null || password.equals(""))
            return "<h1>密码不能为空</h1>";
        if(username.equals(password))
            return "<h1>登录成功</h1>";
        else
            return "<h1>登录失败</h1>";
    }
}